% optimize the network based on sparseness measure
function optimize_map

clear lowest_cost

global Ne IN N A AI Achand best

% call simulator once to define the area sizes as globals.

spnet_som_2_types;

% Parallel Toolbox usage.
if matlabpool('size') == 0
%    matlabpool open 8
end


max_conn_data(1,:) = 300;
max_conn_data(2,:) = 300;
max_conn_data(3,:) = 300;
max_conn_data(4,:) = 300;
max_conn_data(5,:) = 300;
max_conn_data(6,:) = 300;

min_conn_data(1,:) = .001;
min_conn_data(2,:) = .001;
min_conn_data(3,:) = .001;
min_conn_data(4,:) = .001;
min_conn_data(5,:) = .001;
min_conn_data(6,:) = .001;

min_conn_data
max_conn_data

best_params=[   78.8250   27.7434   69.2628   13.0762   74.0525  182.5847]';

conn_data=best_params;



%connect(AI,A,'gauss', 'std', conn(1), 'mininitw', conn(2), 'maxinitw',
%conn(3), 'radius', conn(4), 'delaymin', conn(5), 'delaymax', conn(6), 'lrate', conn(7), 'minw', conn(8), 'maxw', conn(9));

lowest_cost = 1e6;
for i = 1:1
    
    conn_data
    
%     % evolutionary search strategy combined with local min search.
%     opts.LBounds = min_conn_data(:); opts.UBounds = max_conn_data(:); 
%     %opts.Noise.on=1; 
%     sigma = abs(max_conn_data(:)-min_conn_data(:))*.5;
%     [best_params, cost_measure] = cmaes( opt_fun, conn_data(:), sigma, opts, min_conn_data, max_conn_data);

    
    
    [best_params, cost_measure] = fmincon( @spnet_som_2_types, conn_data, [], [] , [],[],min_conn_data, max_conn_data);



    if cost_measure < lowest_cost
        lowest_cost = cost_measure
        save 'best' best_params cost_measure;
    end
    
    
    [cost sp]=spnet_som_2_types(best_params);
    plot(sp(:,1),sp(:,2),'.');

end



function new_conn = tweak(conn_data)

global Ne IN N A AI



new_conn(1,:) = conn_data(1,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(2,:) = conn_data(2,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(3,:) = conn_data(3,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(4,:) = conn_data(4,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(5,:) = conn_data(5,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];
new_conn(6,:) = conn_data(6,:) + [ rand*5-2.5 0 rand*2-1 rand*2-1 round(rand*2-1) round(rand*2-1) rand*.01-.005 rand*.1 rand*1];

% max wt should be >= min wt.
bad_indices = conn_data(:,9) < new_conn(:,8);
new_conn( bad_indices, 8 ) = new_conn(bad_indices, 9); 

bad_indices = conn_data(:,6) < new_conn(:,5);
new_conn( bad_indices, 5 ) = new_conn(bad_indices, 6); 


max_conn_data(1,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(2,:) = [30 5 20 length(AI)/2 5 5 .1 5 20];
max_conn_data(3,:) = [30 5 20 length(AI)/2 5 5 .1 5 20];
max_conn_data(4,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(5,:) = [30 5 20 length(A)/2 5 5 .1 5 20];
max_conn_data(6,:) = [30 0 0 length(A)/2 5 5 .1 5  0];

min_conn_data(1,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(2,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(3,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(4,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(5,:) = [.5 0 1 1 1 1 0 0 0];
min_conn_data(6,:) = [.5 -15 -20 1 1 1 0 -20 0];

new_conn = max(min_conn_data,min(max_conn_data,new_conn));
%connect(IN,A,'gauss', 'std', conn(1), 'mininitw', conn(2), 'maxinitw', conn(3), 'radius', conn(4), 'delaymin', conn(5), 'delaymax', conn(6), 'lrate', conn(7), 'minw', conn(8), 'maxw', conn(9));



